package com.sun.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.List;

/**
 * @author SunRui
 * @description TODO
 * @date 2024/9/30 10:43
 */
@Configuration
@EnableWebMvc
@ComponentScan({"com.sun.mapper"})
public class MvcConfiguration implements WebMvcConfigurer {
    public MvcConfiguration(){
        System.out.println("mvc configuration constructor...");
    }
    //    通过@EnableWebMVC配置的时候起作用，
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for(HttpMessageConverter httpMessageConverter:converters){
            if(StringHttpMessageConverter.class.isAssignableFrom(httpMessageConverter.getClass())){
                ((StringHttpMessageConverter)httpMessageConverter).setDefaultCharset(Charset.forName("UTF-8"));
            }
        }
    }

}